package com.shake;

import android.app.Activity;
import android.graphics.Color;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ShakeShakeActivity extends Activity {
    
	SensorManager sm = null;
    private TextView[] blocks = new TextView[10] ;
    private Handler handler = new Handler() ;
    private int currentPosition = 0 ;
    private boolean reverseFlag = false ;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sm = (SensorManager) getSystemService(SENSOR_SERVICE);
        setContentView(R.layout.main);
        
        blocks[0] = (TextView)findViewById(R.id.textView01) ; 
        blocks[1] = (TextView)findViewById(R.id.textView02) ; 
        blocks[2] = (TextView)findViewById(R.id.textView03) ; 
        blocks[3] = (TextView)findViewById(R.id.textView04) ; 
        blocks[4] = (TextView)findViewById(R.id.textView05) ; 
        blocks[5] = (TextView)findViewById(R.id.textView06) ; 
        blocks[6] = (TextView)findViewById(R.id.textView07) ; 
        blocks[7] = (TextView)findViewById(R.id.textView08) ; 
        blocks[8] = (TextView)findViewById(R.id.textView09) ; 
        blocks[9] = (TextView)findViewById(R.id.textView10) ; 

        handler.postDelayed(blink, 25) ;
    }
    
    public void onSensorChanged(int sensor, float[] values) {
    	if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
    		
    	}
    }
    
    private Runnable blink = new Runnable() {
        @Override
        public void run() {
            if(currentPosition>=9){
                reverseFlag = true ;
            }
            if(reverseFlag == true && currentPosition == 0){
                reverseFlag =false ;
            }

            ShakeShakeActivity.this.lightBlock(reverseFlag?--currentPosition : ++currentPosition) ;
            
            handler.postDelayed(this, 100) ;
        }
    } ;
    
    public void lightBlock(int position){
        for(int i=0; i<10 ;i++){
            if(i != position){
                blocks[i].setBackgroundColor(Color.BLACK);
            }else{
                blocks[i].setBackgroundColor(Color.WHITE);
            }
        }
    }
    
}